import com.mochimedia.ui.*;

class com.mochimedia.ui.RadioButton extends Component {
    
    private var _type:String = "radiobutton";
    
    public function get value ():String { if (_grouped) return _group.value else if (_checked) return _value else return ""; }
 
    public static var groups:Object;
    
    private var _textLabel:String;
    private var _groupName:String;
    private var _grouped:Boolean = false;
    private var _group:Object;
	
	private var _tf:TextField;
	private var _back:MovieClip;
	private var _checkSymbol:MovieClip;
	private var _button:MovieClip;
	private var _fieldbutton:MovieClip;

    private var _highlight:MovieClip;
	
	private var _checkedAtStart:Boolean = false;
	private var _checked:Boolean = false;
    
    public function get text ():String { return _tf.text; }
    public function set text (val:String):Void { _tf.text = val; }

    public function get changed ():Boolean { return (_checkedAtStart != _checked); }
    public function get checked ():Boolean { return _checked; }
    public function set checked (val:Boolean):Void { 
        if (val != _checked) toggle();
	} 
    
    public var validate:Function;
    
    
    public function RadioButton (container:MovieClip, textLabel:String, value:String, groupName:String, checked:Boolean, width:Number, height:Number, position:Position, style:Style) {
        
        init (container, textLabel, value, groupName, checked, width, height, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, textLabel:String, value:String, groupName:String, checked:Boolean, width:Number, height:Number, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
		_textLabel = textLabel;
        _value = (value != undefined) ? value : textLabel.toLowerCase();

        _groupName = (groupName.length > 0) ? groupName : "";
        if (checked == undefined) checked = false;
        _checkedAtStart = _checked = checked;
        _width = width;
        _height = height;
        
        if (_groupName.length > 0) {
            
            _grouped = true;
            
            if (RadioButton.groups == null) RadioButton.groups = { };
            
            if (RadioButton.groups[_groupName] == null) {
                RadioButton.groups[_groupName] = { };
                _checked = true;
            }
           
            _group = RadioButton.groups[_groupName];
            
            if (_checked) {
                if (_group.activeRadioButton != null) _group.activeRadioButton.checked = false;
                _group.activeRadioButton = this;
                _group.value = this.value;
            }
            
        }
 
    }
    
    //
    //
    public function create ():Void {

        super.create();
     
        if (isNaN(_width)) _width = _parentCell.width - _position.margin_left - _position.margin_right;
        
        _tf = Create.newText(_textLabel, "labelTF", _mc, _style, null, null, true);
        _tf._x = 6;
        _tf._y = 0;
        _tf.tabIndex = id;
		_tf.selectable = false;
		_tf.tabEnabled = false;
		_tf.multiline = true;
		_tf.wordWrap = true;
		_tf._x = 20;
		
		_back = _mc.createEmptyMovieClip("_back", _mc.getNextHighestDepth());
        DrawingMethods.circle(_back, false, 8, 8, 6, 16, _style.backgroundColor, 100, 2,  ColorTools.getTintedColor(_style.borderColor, _style.backgroundColor, 0.7), 100);
        
		if (!isNaN(_width)) _tf._width = _width - 16 - 4;
		
        if (isNaN(_height)) {
            _height = _tf._height;
        } else {
            _tf._height = _height - 4;
        }
		
		_checkSymbol = _mc.createEmptyMovieClip("check", _mc.getNextHighestDepth());
        DrawingMethods.circle(_checkSymbol, false, 8, 8, 6, 16, _style.backgroundColor, 0, 2, _style.borderColor, 100);
        DrawingMethods.circle(_checkSymbol, false, 8, 8, 3, 16, _style.buttonColor, 100);

		_checkSymbol._visible = checked;

        _width = _mc._width;
		
        _highlight = _mc.createEmptyMovieClip("_highlight", _mc.getNextHighestDepth());
        DrawingMethods.emptyRect(_highlight, false, 0, 0, 16, 16, 2, _style.borderColor);
        DrawingMethods.emptyRect(_highlight, false, -2, -2, 20, 20, 2, ColorTools.getSaturatedColor(_style.haloColor, 255), 60);
        _highlight._visible = false;
		
		_button = _mc.createEmptyMovieClip("_button", _mc.getNextHighestDepth());
		DrawingMethods.rect(_button, false, 0, 0, 16, 16, 0xffffff, 0.2);
		
		_back._y = _checkSymbol._y = _button._y = _highlight._y = Math.max(0, Math.floor((_tf._height - 16) * 0.5));
        
		connectButton(_button);
        addEventListener(EVENT_CLICK, this, toggle);
		
		_mc.tabChildren = _button.tabEnabled = true;
		_button.tabIndex = _id;

		var self:Object = this;
		_button.onSetFocus = function ():Void { self._highlight._visible = true; }
		
		_fieldbutton = _mc.createEmptyMovieClip("_fieldbutton", _mc.getNextHighestDepth());
		DrawingMethods.rect(_fieldbutton, false, _tf._x - 4, _tf._y, _tf._width + 4, _tf._height, 0xffffff, 0);
		
		connectButton(_fieldbutton);
		_fieldbutton.tabEnabled = false;

    }
 
	//
    //
    public function toggle (e:Event):Void {

        if (e != undefined && _checked && _grouped && _group.activeRadioButton == this) return;
        
        if (!_checked && _grouped) {
            if (_group.activeRadioButton != null) RadioButton(_group.activeRadioButton).checked = false;
            _group.activeRadioButton = this;
            _group.value = _value;
        }
        
		_checked = _checkSymbol._visible = (!_checked);
		
		if (_checked || !_grouped) dispatchEvent(EVENT_CHANGE);
        
    }
	
    
    //
    //
    public function onFocus (e:Event):Void {
        
        if (_enabled) {
            
            _highlight._visible = true;
            
        }
    
    }
    
    //
    //
    public function onBlur (e:Event):Void {
        
        _highlight._visible = false;
        
    }
    
    
    //
    //
    public function enable ():Void {
        
        super.enable();
        
        _tf.textColor = _style.textColor;
		_button.tabEnabled = true;
        
    }
    
    //
    //
    public function disable ():Void {
        
        super.disable();
        
        _tf.textColor = ColorTools.getTintedColor(_style.textColor, _style.backgroundColor, 0.8);
        _highlight._visible = false;
		_button.tabEnabled = false;
        
    }
    
    //
    //
    public function lock ():Void {
        
        disable();
        
        _tf.textColor = ColorTools.getTintedColor(_style.textColor, _style.backgroundColor, 0.4);
        
    }
    
}